package com.jfreer.security;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;

/**
 * User: landy
 * Date: 15/6/1
 * Time: 下午10:41
 */
public class MyInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    @Resource
    private SecurityData securityData;

    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        FilterInvocation fi = (FilterInvocation) o;
        String requestUrl = fi.getRequestUrl();
        System.out.println("requestUrl:" + requestUrl);
        Collection<ConfigAttribute> rst = new ArrayList<ConfigAttribute>();
        for (String role : securityData.getRoleListByUrl(requestUrl)) {
            rst.add(new SecurityConfig(role));
        }
        return rst;
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return aClass == FilterInvocation.class;
    }
}
